<?php

namespace app\api\controller;
header('content-type:application:json;charset=utf8');  
header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Credentials:true');  
header('Access-Control-Allow-Methods:*');  
header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
use app\common\controller\Api;
use think\Db;
use think\Session;
use think\Request;
use think\Cache;
use think\Config;

/**
 * @title 存储接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function __construct($ids)
    {
        $id = Cache::get('userid'.$ids);
        $userid = Db::name('user')->field('id')->where(['id'=>$id])->find();
        if(!$id && !$userid){
            $arr['code'] = 0;
            $arr['msg'] = '请先登录';
            echo json_encode($arr);
            die;
        }
    }

    /**
     * @title 提交分数
     * @url /api/Index/SetScore
     * @method post
     * @param name:ids type:int require:1 desc:用户ID
     * @param name:score type:int require:1 desc:分数
     * @param name:totalscore type:int require:1 desc:加密分数
     * @return Score:单次分数
     * @return MaxScore:最佳分数
     * @return Sort:排名
     */
    public function SetScore($ids,$score,$totalscore){
        if(Request::instance()->post()){
            $time = strtotime(date('Y-m-d H:i:s'));
            $startdate = strtotime(Config::get('date.startdate'));
            $enddate = strtotime(Config::get('date.enddate'));
            if($time < $startdate){
                $this->error('活动未开始',false,'200','json');
            }elseif($time > $startdate && $time < $enddate){
                $rand = Cache::get('userrand'.$ids);
                $totalscores = $score*$rand;
                if($totalscore = $totalscores){
                    $id =Cache::get('userid'.$ids);
                    $dadystart = strtotime(date('Y-m-d 0:0:0'));
                    $dadyend = strtotime(date('Y-m-d 23:0:0'));
                    $where['createtime'] = ['between',[$dadystart,$dadyend]];
                    $where['user_id'] = $id;
                    $count = Db::name('particular')->where($where)->count();
                    if($count >= 4){
                        $this->error('今日游戏已达上限,请明日再来',false,'200','json');
                    }else{
                        $wherecount['createtime'] = ['between',[$dadystart,$dadyend]];
                        $wherecount['friend_id'] = $id;
                        $countres = Db::name('resurgence')->where($wherecount)->count();
                        $res = $countres + 1;
                        if($count < $res){
                            $result = 0;
                            $insertData = [
                                'user_id' => $id,
                                'score' => $score,
                                'createtime' => $time,
                            ];
                            $result += Db::name('particular')->insert($insertData);
                            $wherepoints['user_id'] = $id;
                            $points = Db::name('points')->where($wherepoints)->find();
                            if($points){
                                if($points['total_score'] < $score){
                                    $updatepoints = [
                                        'total_score' => $score,
                                        'updatetime' => $time,
                                    ];
                                    $result += Db::name('points')->where(['user_id'=>$id])->update($updatepoints);
                                }
                            }else{
                                $insertponits = [
                                    'user_id' => $id,
                                    'total_score' => $score,
                                    'createtime' => $time,
                                    'updatetime' => $time,
                                ];
                                $result += Db::name('points')->insert($insertponits);
                            }
                            if($result > 0){
                                $arr = [];
                                $arr['Score'] = $score;
                                $maxscore = Db::name('points')->where(['user_id'=>$id])->find();
                                $arr['MaxScore'] = $maxscore['total_score'];
                                $sortwhere['total_score'] = ['>',$maxscore['total_score']];
                                $sort = Db::name('points')->where($sortwhere)->count();
                                $arr['Sort'] = $sort + 1;
                                $this->success('提交成功',$arr,'200','json');
                            }else{
                                $arr = [];
                                $arr['Score'] = $score;
                                $maxscore = Db::name('points')->where(['user_id'=>$id])->find();
                                $arr['MaxScore'] = $maxscore['total_score'];
                                $sortwhere['total_score'] = ['>',$maxscore['total_score']];
                                $sort = Db::name('points')->where($sortwhere)->count();
                                $arr['Sort'] = $sort + 1;
                                $this->error('提交失败',$arr,'200','json');
                            }
                        }else{
                            $this->error('网络错误',false,'200','json');
                        }
                    }
                }else{
                    $this->error('成绩有误',false,'200','json');
                }
                
            }else{
                $this->error('活动已结束',false,'200','json');
            }
           
            
        }
            
    }



    /**
     * @title 提交好友复活信息
     * @url /api/Index/SetFriend
     * @method post
     * @param name:ids type:int require:1 desc:用户ID
     * @param name:friendID type:int require:1 desc:被复活好友ID
     */
    public function SetFriend($ids,$friendID){
        if(Request::instance()->post()){
            $time = strtotime(date('Y-m-d H:i:s'));
            $startdate = strtotime(Config::get('date.startdate'));
            $enddate = strtotime(Config::get('date.enddate'));
           if($time < $startdate){
               $this->error('活动暂未开始',false,'200','json');
           }elseif($time > $startdate && $time < $enddate){
            $fp = fopen('lock.txt','w+');
            if(flock($fp,LOCK_EX | LOCK_NB)){
                if(is_numeric($friendID)){
                    $dadystart = strtotime(date('Y-m-d 0:0:0'));
                    $dadyend = strtotime(date('Y-m-d 23:0:0'));
                    $where['createtime'] = ['between',[$dadystart,$dadyend]];
                    $where['friend_id'] = $friendID;
                    $where['user_id'] = Cache::get('userid'.$ids);
                    $find = Db::name('resurgence')->where($where)->find();
                    if($find){
                        $this->error('已为此好友助力过',false,'200','json');
                    }else{
                        $wherecount['createtime'] = ['between',[$dadystart,$dadyend]];
                        $wherecount['friend_id'] = $friendID;
                        $count = Db::name('resurgence')->where($wherecount)->count();
                        if($count >= 3){
                            $this->error('此好友今日助力已满，请明日再来',false,'200','json');
                        }else{
                            $insertData = [
                                'user_id' => Cache::get('userid'.$ids),
                                'friend_id' => $friendID,
                                'createtime' => strtotime(date('Y-m-d H:i:s')),
                            ];
                            $result = Db::name('resurgence')->insert($insertData);
                            if($result){
                                $id = Cache::get('userid'.$friendID);
                                $time = strtotime(date('Y-m-d'));
                                $res = Db::name('share')->field('share')->where(['user_id'=>$id,'createtime'=>$time])->find();
                                if($res){
                                    $updatedata['share'] = $res['share']+1;
                                    $result = Db::name('share')->where(['user_id'=>$id,'createtime'=>$time])->update($updatedata);
                                    //var_dump($result);
                                }else{
                                    $insertdata['share'] = 1;
                                    $insertdata['createtime'] = $time;
                                    $insertdata['user_id'] = $id;
                                    $insertdata['updatetime'] = strtotime(date('Y-m-d H:i:s'));
                                    $result = Db::name('share')->insert($insertdata);
                                    //var_dump(2);
                                }
                                $this->success('提交成功',true,'200','json');
                            }else{
                                $this->error('提交失败',false,'300','json');
                            }
                        }
                        
                    }
                
                }else{
                    $this->error('情输入正确的用户ID',false,'200','json');
                }
                flock($fp,LOCK_UN);
             }else{
                $this->error('系统繁忙，请稍后再试',false,'200','json');
             }
           }else{
               $this->error('活动已结束',false,'200','json');
           }
        }
    }


    /**
     * @title 记录每天分享次数
     * @url /api/Index/SetShare
     * @method post
     * @param name:ids type:int require:1 desc:用户ID
     */
    public function SetShare($ids){
        $id = Cache::get('userid'.$ids);
        $time = strtotime(date('Y-m-d'));
        $res = Db::name('share')->field('share')->where(['user_id'=>$id,'createtime'=>$time])->find();
        if($res){
            $updatedata['share'] = $res['share']+1;
            $result = Db::name('share')->where(['user_id'=>$id,'createtime'=>$time])->update($updatedata);
            //var_dump($result);
        }else{
            $insertdata['share'] = 1;
            $insertdata['createtime'] = $time;
            $insertdata['user_id'] = $id;
            $insertdata['updatetime'] = strtotime(date('Y-m-d H:i:s'));
            $result = Db::name('share')->insert($insertdata);
             //var_dump(2);
        }
        if($result){
            $this->success('提交成功',true,'200','json');
        }else{
            $this->success('提交失败'.false,'300','json');
        }
    }






}
